#!/bin/sh

echo "loading circuit environment..."

home=$HOME
src=$home/genus/misc/vagrant
gocircuit=$home/gocircuit

# Install environment
cd $home
cp $src/.profile .profile
. .profile

# Set up mount point for the circuit
mkdir /circuit
chown vagrant /circuit
chgrp fuse /circuit
addgroup vagrant fuse

# fuse.conf
cp $src/fuse.conf /etc/fuse.conf
chown vagrant /etc/fuse.conf
chgrp fuse /etc/fuse.conf
chmod 0660 /etc/fuse.conf

# Freshen up the box; non-interactive mode?
apt-get -y update
apt-get -y install vim
apt-get -y install mercurial
apt-get -y install git
apt-get -y install make

# Fetch, build, install Go

if [ "$(ls -A $home/go)" ]
then
  echo "Go already installed."
else
  echo "Fetching and building Go..."
  cd $home
  hg clone https://code.google.com/p/go/
  cd $home/go/src
  ./all.bash || exit
fi
export PATH=$PATH:$home/go/bin

# Build, and install the circuit
echo Building circuit...
if [ "$(ls -A $gocircuit)" ]
then
  echo "circuit already installed."
else
  echo "fetching and building the circuit..."
  cd $home
  hg clone https://code.google.com/p/gocircuit/
  cd $gocircuit/src/circuit/cmd/circuit
  go install || exit
fi

# Pre-fetch base docker image
sudo docker pull ubuntu

echo "circuit installed successfully."
